package edu.colorado.karl.intelsched;

//TODO: Change nn from null to new neural network, run that neural network.
//TODO: Make getSetPoint actually return data
//TODO: Make 


/**
 * Class to return intelligently built set points using back-propagation
 * neural networks.
 * @author Andrew Boehm
 */
public class SetPointEvaluator {
	
	private Weather forecast;
	private ISInfo current;
	private ISInfo previous;
	private RSSReader reader;
	
	private int coldSetList[] = {60,55,50,45,41,38,35,32};
	private int hotSetList[] = {80,82,84,86,90,94,98,102,106,110,115,120};
	
	
	/**
	 * Instantiate object to return intelligently-built set points
	 * @param current ISInfo from most recent house data
	 * @param previous ISInfo from second-most recent house data
	 */
	public SetPointEvaluator(ISInfo current, ISInfo previous)
	{
		reader = new RSSReader();
		this.forecast = reader.getPredictedWeather();
		this.current = current;
		this.previous = previous;
	}
	
	/**
	 * Creates a {@link SPData} set containing set points from the Intelligent
	 * Scheduler
	 * @return An SPData containing new tank set points for the hot and cold tanks
	 */
	public SPData getSetPoint()
	{
		Integer hotSetP;
		Integer coldSetP;
		if(current.getDayOfYear()>180 || current.getDayOfYear() <300) //heating mode
		{
			Integer coldindex = 0;;
			if(forecast.getHighTemperature() > current.getRoomSetpoint())
				coldindex+=(int)(forecast.getHighTemperature()-current.getRoomSetpoint())/5;
			if(forecast.isSunny())
				coldindex++;
			if(forecast.isPartlyCloudy())
				coldindex--;
			if(forecast.isMostlyCloudy())
				coldindex-=2;

			if(current.getHourOfDay() < 5 || current.getHourOfDay() > 22)
			{
				hotSetP = forecast.getLowTemperature() + 10;
				coldindex++;
			}
			else
				hotSetP = forecast.getHighTemperature() + 15;
			if(coldindex < 0)
				coldindex = 0;
			if(coldindex > 7)
				coldindex = 7;
			coldSetP = coldSetList[coldindex];
		}
		
		else
		{
			Integer hotindex = 0;
			if(forecast.getHighTemperature() > current.getRoomSetpoint())
				hotindex+=(int)(current.getRoomSetpoint()-forecast.getHighTemperature())/5;
			if(forecast.isPartlyCloudy())
				hotindex++;
			if(forecast.isMostlyCloudy())
				hotindex+=2;
			if(hotindex<0)
				hotindex = 0;
			if(hotindex > 11)
				hotindex = 11;
			if(current.getHourOfDay()<8 || current.getHourOfDay()>20)
				coldSetP = forecast.getLowTemperature()-5;
			else
				coldSetP = forecast.getHighTemperature()-5;
			hotSetP = hotSetList[hotindex];
			
		}
		SPData set = new SPData(forecast,coldSetP,hotSetP,
			!(current.getDayOfYear()>180 || 
			current.getDayOfYear()<300));
		return set;
	}
	 
	/**
	 * Updates the weather forecast for this instance of the Intelligent
	 * Scheduler.
	 * The weather update should be run daily at 6 AM and again at 6 PM.
	 *
	 */
	public void updateWeather()
	{
		this.forecast = reader.getPredictedWeather();
	}
	
	/**
	 * Updates the Intelligent Scheduler with information from the previous hour.
	 * @param current The most recent complete intelligent scheduler data from 
	 * the database.
	 * @see ISInfo
	 */
	public void updateISinfo(ISInfo current)
	{
		this.previous = this.current;
		this.current = current;
	}
}
